<?php

class Dispatch {
    
    /*
	 * 路由分发器
	 */
    const DS = DIRECTORY_SEPARATOR; # '/' 目录路径
    const DEFAULT_CONTROLLER = 'default'; # 默认目录
    const DEFAULT_ACTION = 'index'; # 默认action
    const DEFAULT_SUFFIX = 'Action'; # 后缀׺

    public static function getDispatch ($query, $path) {
        $query = explode(DS, $query);
        if (count($query) == 1) {
            $controller = $query[0];
        } else {
            $controller = self::DEFAULT_CONTROLLER;
        }
        if (count($query) == 2) {
            if (empty($query[0]) || !ctype_alpha($query[0]) || empty($query[1]) || !ctype_alpha($query[1])) {
                return FALSE;
            }
            $controller = $query[0];
            $action = $query[1];
        }
        
        $className = ucfirst((isset($action)) ? $action : self::DEFAULT_ACTION) . self::DEFAULT_SUFFIX;
        $classPath = $path . DS . strtolower($controller) . DS . $className . '.php';
        
        if (!file_exists($classPath)) {
            return FALSE;
        }
        require $classPath;
        $object = new $className();
        return $object;
    }
}
?>